﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class SkirmishMenu : Menu
    {
        // Event for select main menu
        public delegate void EventHandleMenuSelect(int choice);
        public event EventHandleMenuSelect MenuSelect;

        private SkirmishMenuWithXML _skirmishMenuWithXMl;
        private GameWindow _window;
        private List<MyButton> _listMenuOption;
        private SkirmishSetup _skirmishSetup;

        private MyButton leftMoney, rightMoney, leftUnitCap, rightUnitCap;

        public SkirmishSetup SkirmishSetup
        {
            get { return _skirmishSetup; }
            set { _skirmishSetup = value; }
        }

        public List<MyButton> ListMenuOption
        {
            get { return _listMenuOption; }
            set { _listMenuOption = value; }
        }

        private Sprite2D money_UnitCap;
        private List<NationSetupMenu> listNationMenu;
        private List<Sprite2D> listMoney, listUnitcap;

        public SkirmishMenu(GameWindow window, ContentManager content, GraphicsDevice graphicsDevice, string strmenuxml, int nTeam)
        {
            _window = window;
            _skirmishMenuWithXMl = content.Load<SkirmishMenuWithXML>(strmenuxml);
            SkirmishSetup = new SkirmishSetup(nTeam);
            SizeScale = _skirmishMenuWithXMl.sizeScale;
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            InitMenu(content, graphicsDevice,nTeam);
        }

        public void InitMenu(ContentManager content, GraphicsDevice graphicsDevice, int nTeam)
        {
            Background = LoadStaticItem(content, _skirmishMenuWithXMl.strBackground, _skirmishMenuWithXMl.sizeScale, new Vector2(0,0), "", new Vector2(0,0));

            money_UnitCap = LoadStaticItem(content, _skirmishMenuWithXMl.strMoney_UnitCap, _skirmishMenuWithXMl.sizeScaleMoney_UnitCap, _skirmishMenuWithXMl.positionScaleMoney_UnitCap, "", new Vector2(0, 0));

            listNationMenu = new List<NationSetupMenu>();
            for (int i = 0; i < nTeam; i++)
            {
                NationSetupMenu tempNationSetup = new NationSetupMenu(content, graphicsDevice, "XML/NationSetupMenuWithXML", new NationSetup(i));
                listNationMenu.Add(tempNationSetup);
                listNationMenu[i].ClickButton += new NationSetupMenu.EventHandleClick(NationSetupChange);
            }

            ListMenuOption = new List<MyButton>();
            ListMenuOption = LoadListItem(content, _skirmishMenuWithXMl.spriteMenuXML, "SkirmishMenu/MenuOption/", new Vector2(0,0));

            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);        
            }

            leftMoney = LoadItem(content, _skirmishMenuWithXMl.leftMoney, "SkirmishMenu/MenuOption/", new Vector2(0, 0));
            leftMoney.ClickButton += new MyButton.EventHandleClick(ClickLeftMoney);               

            rightMoney = LoadItem(content, _skirmishMenuWithXMl.rightMoney, "SkirmishMenu/MenuOption/", new Vector2(0, 0));
            rightMoney.ClickButton += new MyButton.EventHandleClick(ClickRightMoney);              
 
            leftUnitCap = LoadItem(content, _skirmishMenuWithXMl.leftUnitCap, "SkirmishMenu/MenuOption/", new Vector2(0, 0));
            leftUnitCap.ClickButton += new MyButton.EventHandleClick(ClickLeftUnitCap); 

            rightUnitCap = LoadItem(content, _skirmishMenuWithXMl.rightUnitCap, "SkirmishMenu/MenuOption/", new Vector2(0, 0));
            rightUnitCap.ClickButton += new MyButton.EventHandleClick(ClickRightUnitCap);              

            listMoney = new List<Sprite2D>();
            listMoney = LoadStaticListItem(content, _skirmishMenuWithXMl.listMoney, "SkirmishMenu/");

            listUnitcap = new List<Sprite2D>();
            listUnitcap = LoadStaticListItem(content, _skirmishMenuWithXMl.listUnitcap, "SkirmishMenu/");
        }

        //Process when option was clicked
        public void ClickButtonWithSender(object sender)
        {
            int choice = ListMenuOption.IndexOf((MyButton)sender);
            if (choice != -1) MenuSelect(choice);
        }

        // Procces whele click left Money
        public void ClickLeftMoney()
        {
            _skirmishSetup.Update(SkirmishSetup.TypeUpdate.UpdateMoney, SkirmishSetup.Navigate.Back);
        }

        // Procces whele click right Money
        public void ClickRightMoney()
        {
            _skirmishSetup.Update(SkirmishSetup.TypeUpdate.UpdateMoney, SkirmishSetup.Navigate.Next);
        }

        // Procces whele click left UnitCap
        public void ClickLeftUnitCap()
        {
            _skirmishSetup.Update(SkirmishSetup.TypeUpdate.UpdateUnitCap, SkirmishSetup.Navigate.Back);
        }

        // Procces whele click right UnitCap
        public void ClickRightUnitCap()
        {
            _skirmishSetup.Update(SkirmishSetup.TypeUpdate.UpdateUnitCap, SkirmishSetup.Navigate.Next);
        }

        // Proccess when change setting nation
        public void NationSetupChange(object sender)
        {
            int choice = listNationMenu.IndexOf((NationSetupMenu)sender);
            if (choice != -1)
            {
                SkirmishSetup.ListNationSetup[choice] = ((NationSetupMenu)sender).NationSetup;
            }
        }

        public void UpdateMenuSize(GraphicsDevice graphicsDevice)
        {
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            Background.Size = Size;

            money_UnitCap.Size = new Vector2(_skirmishMenuWithXMl.sizeScaleMoney_UnitCap.X * Size.X, _skirmishMenuWithXMl.sizeScaleMoney_UnitCap.Y * Size.Y);
            money_UnitCap.Position = new Vector2(_skirmishMenuWithXMl.positionScaleMoney_UnitCap.X * Size.X, _skirmishMenuWithXMl.positionScaleMoney_UnitCap.Y * Size.Y);

            // Update new  size and position for money
            leftMoney.SetSize(new Vector2(_skirmishMenuWithXMl.leftMoney.sizeScale.X * Size.X, _skirmishMenuWithXMl.leftMoney.sizeScale.Y * Size.Y));
            leftMoney.SetPosition(new Vector2(_skirmishMenuWithXMl.leftMoney.positionScale.X * Size.X, _skirmishMenuWithXMl.leftMoney.positionScale.Y * Size.Y));

            rightMoney.SetSize(new Vector2(_skirmishMenuWithXMl.rightMoney.sizeScale.X * Size.X, _skirmishMenuWithXMl.rightMoney.sizeScale.Y * Size.Y));
            rightMoney.SetPosition(new Vector2(_skirmishMenuWithXMl.rightMoney.positionScale.X * Size.X, _skirmishMenuWithXMl.rightMoney.positionScale.Y * Size.Y));

            for (int i = 0; i < listMoney.Count; i++)
            {
                listMoney[i].Size = new Vector2(_skirmishMenuWithXMl.listMoney[i].sizeScale.X * Size.X, _skirmishMenuWithXMl.listMoney[i].sizeScale.Y * Size.Y);
                listMoney[i].Position = new Vector2(_skirmishMenuWithXMl.listMoney[i].positionScale.X * Size.X, _skirmishMenuWithXMl.listMoney[i].positionScale.Y * Size.Y);
            }

            // Update new  size and position for unitcap
            leftUnitCap.SetSize(new Vector2(_skirmishMenuWithXMl.leftUnitCap.sizeScale.X * Size.X, _skirmishMenuWithXMl.leftUnitCap.sizeScale.Y * Size.Y));
            leftUnitCap.SetPosition(new Vector2(_skirmishMenuWithXMl.leftUnitCap.positionScale.X * Size.X, _skirmishMenuWithXMl.leftUnitCap.positionScale.Y * Size.Y));

            rightUnitCap.SetSize(new Vector2(_skirmishMenuWithXMl.rightUnitCap.sizeScale.X * Size.X, _skirmishMenuWithXMl.rightUnitCap.sizeScale.Y * Size.Y));
            rightUnitCap.SetPosition(new Vector2(_skirmishMenuWithXMl.rightUnitCap.positionScale.X * Size.X, _skirmishMenuWithXMl.rightUnitCap.positionScale.Y * Size.Y));

            for (int i = 0; i < listUnitcap.Count; i++)
            {
                listUnitcap[i].Size = new Vector2(_skirmishMenuWithXMl.listUnitcap[i].sizeScale.X * Size.X, _skirmishMenuWithXMl.listUnitcap[i].sizeScale.Y * Size.Y);
                listUnitcap[i].Position = new Vector2(_skirmishMenuWithXMl.listUnitcap[i].positionScale.X * Size.X, _skirmishMenuWithXMl.listUnitcap[i].positionScale.Y * Size.Y);
            }

            // Update new size and positon for nation menu setup
            for (int i = 0; i < listNationMenu.Count; i++)
            {
                listNationMenu[i].UpdateMenuSize(graphicsDevice);
            }

            // Update new size and position for button
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].SetPosition(new Vector2(_skirmishMenuWithXMl.spriteMenuXML[i].positionScale.X * Size.X, _skirmishMenuWithXMl.spriteMenuXML[i].positionScale.Y * Size.Y));
                ListMenuOption[i].SetSize(new Vector2(_skirmishMenuWithXMl.spriteMenuXML[i].sizeScale.X * Size.X, _skirmishMenuWithXMl.spriteMenuXML[i].sizeScale.Y * Size.Y));
            }
        }

        public override void Update(GameTime gameTime)
        {
            Background.Update(gameTime);

            money_UnitCap.Update(gameTime);

            for (int i = 0; i < listNationMenu.Count; i++)
            {
                listNationMenu[i].Update(gameTime);
            }

            leftMoney.Update(gameTime);
            rightMoney.Update(gameTime);

            switch (SkirmishSetup.Money)
            { 
                case 500:
                    listMoney[0].Update(gameTime);
                    break;
                case 1000:
                    listMoney[1].Update(gameTime);
                    break;
                case 2000:
                    listMoney[2].Update(gameTime);
                    break;
            }

            leftUnitCap.Update(gameTime);
            rightUnitCap.Update(gameTime);

            switch (SkirmishSetup.UnitCap)
            { 
                case 10:
                    listUnitcap[0].Update(gameTime);
                    break;
                case 20:
                    listUnitcap[1].Update(gameTime);
                    break;
                case 25:
                    listUnitcap[2].Update(gameTime);
                    break;
            }

            for (int i = 0; i < 2; i++)
            {
                ListMenuOption[i].Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Background.Draw(gameTime, spriteBatch);

            money_UnitCap.Draw(gameTime, spriteBatch);

            for (int i = 0; i < listNationMenu.Count; i++)
            {
                listNationMenu[i].Draw(gameTime, spriteBatch);
            }

            leftMoney.Draw(gameTime, spriteBatch);
            rightMoney.Draw(gameTime, spriteBatch);

            switch (SkirmishSetup.Money)
            {
                case 500:
                    listMoney[0].Draw(gameTime, spriteBatch);
                    break;
                case 1000:
                    listMoney[1].Draw(gameTime, spriteBatch);
                    break;
                case 2000:
                    listMoney[2].Draw(gameTime, spriteBatch);
                    break;
            }

            leftUnitCap.Draw(gameTime, spriteBatch);
            rightUnitCap.Draw(gameTime, spriteBatch);

            switch (SkirmishSetup.UnitCap)
            {
                case 10:
                    listUnitcap[0].Draw(gameTime, spriteBatch);
                    break;
                case 20:
                    listUnitcap[1].Draw(gameTime, spriteBatch);
                    break;
                case 25:
                    listUnitcap[2].Draw(gameTime, spriteBatch);
                    break;
            }
            for (int i = 0; i < 2; i++)
            {
                ListMenuOption[i].Draw(gameTime, spriteBatch);
            }
        }
    }
}
